package com.yfuxdeer.youchat.service;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

import com.yfuxdeer.youchat.dao.FriendDao;
import com.yfuxdeer.youchat.dao.impl.FriendDaoImpl;
import com.yfuxdeer.youchat.pojo.FriendItem;

import java.io.Serializable;
import java.util.List;

public class FriendService extends Service {
    private static final String TAG = FriendService.class.getSimpleName();
    private static FriendDao friendDao = new FriendDaoImpl();
    private Bundle bundle;
    private Bundle data;

    public enum METHOD {
        FIND_ALL,
        FIND_FRIEND,
        ADD_FRIEND,
        MODIFY_REMARK,
        DELETE_FRIEND
    }

    public enum RESULT {
        SUCCESS,
        NETWORK_EXCEPTION
    }

    public void findAll() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String username = bundle.getString("username");
                    List<FriendItem> friendItemList = friendDao.findAll(username);
                    data.putSerializable("friendItemList", (Serializable) friendItemList);
                    data.putSerializable("result", RESULT.SUCCESS);
                } catch (Exception e) {
                    data.putSerializable("result", RESULT.NETWORK_EXCEPTION);
                }
                Intent broadcast = new Intent("com.yfuxdeer.youchat.findAllFriendReceiver");
                broadcast.putExtras(data);
                sendBroadcast(broadcast);
            }
        }).start();
    }

    public static FriendItem findFriendById(String host_id, String friend_id) throws Exception {
        return friendDao.findById(host_id, friend_id);
    }

    public void findFriendById() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String host_id = bundle.getString("host_id");
                    String friend_id = bundle.getString("friend_id");
                    FriendItem friendItem = friendDao.findById(host_id, friend_id);
                    data.putSerializable("friendItem", friendItem);
                    data.putSerializable("result", RESULT.SUCCESS);
                } catch (Exception e) {
                    data.putSerializable("result", RESULT.NETWORK_EXCEPTION);
                }
                Intent broadcast = new Intent("com.yfuxdeer.youchat.findFriendReceiver");
                broadcast.putExtras(data);
                sendBroadcast(broadcast);
            }
        }).start();
    }

    public void addFriend() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String host_id = bundle.getString("host_id");
                    String friend_id = bundle.getString("friend_id");
                    //双向添加
                    friendDao.add(host_id, friend_id);
                    data.putSerializable("result", RESULT.SUCCESS);
                } catch (Exception e) {
                    data.putSerializable("result", RESULT.NETWORK_EXCEPTION);
                }
                Intent broadcast = new Intent("com.yfuxdeer.youchat.addFriendReceiver");
                broadcast.putExtras(data);
                sendBroadcast(broadcast);
            }
        }).start();
    }

    public void modifyRemark() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String host_id = bundle.getString("host_id");
                    String friend_id = bundle.getString("friend_id");
                    String remark = bundle.getString("remark");
                    friendDao.updateRemarkByIds(host_id, friend_id, remark);
                    data.putString("remark", remark);
                    data.putSerializable("result", RESULT.SUCCESS);
                } catch (Exception e) {
                    data.putSerializable("result", RESULT.NETWORK_EXCEPTION);
                }
                Intent broadcast = new Intent("com.yfuxdeer.youchat.modifyRemarkReceiver");
                broadcast.putExtras(data);
                sendBroadcast(broadcast);
            }
        }).start();
    }

    public void deleteFriend() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String host_id = bundle.getString("host_id");
                    String friend_id = bundle.getString("friend_id");
                    friendDao.delete(host_id, friend_id);
                    data.putString("friend_id", friend_id);
                    data.putSerializable("result", RESULT.SUCCESS);
                } catch (Exception e) {
                    data.putSerializable("result", RESULT.NETWORK_EXCEPTION);
                }
                Intent broadcast = new Intent("com.yfuxdeer.youchat.deleteFriendReceiver");
                broadcast.putExtras(data);
                sendBroadcast(broadcast);
            }
        }).start();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "onStartCommand: " + "接收到服务请求");
        bundle = intent.getExtras();
        data = new Bundle();
        FriendService.METHOD method = (FriendService.METHOD) bundle.getSerializable("method");
        switch (method) {
            case FIND_ALL:
                findAll();
                break;
            case ADD_FRIEND:
                addFriend();
                break;
            case FIND_FRIEND:
                findFriendById();
                break;
            case DELETE_FRIEND:
                deleteFriend();
                break;
            case MODIFY_REMARK:
                modifyRemark();
                break;
        }
        stopSelf();
        return super.onStartCommand(intent, flags, startId);
    }
}
